<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/8/2
 * Time: 15:22
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Brand extends Base
{
    /**
     * 产品首页
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $brand_name = trim(input('brand_name'));
            $brand_status = input('brand_status');
            $brand_cate = input('brand_cate');
            
            $where['brand_status'] = ['between', [1, 2]];
            if(isset($brand_name) && $brand_name != ''){
                $where['brand_name'] = ['like', '%' . $brand_name . '%'];
            }
            if(isset($brand_status) && $brand_status != ''){
                $where['brand_status'] = $brand_status;
            }
            if(isset($brand_cate) && $brand_cate != ''){
                $where['brand_cate_id'] = ['like', '%,' . $brand_cate . ',%'];
            }
        
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('brand')->where($where)->count();// 获取总条数
            $lists = Db::name('brand')
                ->field('brand_id, brand_name, brand_image, brand_sort, brand_cate_id, 
                brand_status, brand_time')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('brand_sort desc')
                ->select();
            if($lists){
                foreach($lists as $k => $v){
                    $cate_id = trim($v['brand_cate_id'], ',');
                    $cate_name = Db::table('up_cate')->where(['cate_id' => ['IN', $cate_id]])->column('cate_name');
                    $lists[$k]['cate_name'] = implode(',', $cate_name);
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $cate = Db::table('up_cate')->where('cate_status', 1)->field('cate_id, cate_name')->select();
        $this->assign('cate', $cate);
        return $this->fetch();
    }
    
    /**
     * 添加品牌
     * @return mixed|\think\response\Json
     */
    public function brandAdd(){
        if(Request::instance()->isPost()){
            $brand_name = trim(input('brand_name'));
            $brand_image = input('brand_image');
            $brand_sort = input('brand_sort');
            $brand_cate_id = input('brand_cate_id');
            $brand_status = input('brand_status');
            
            $insert = [
                'brand_name' => $brand_name,
                'brand_image' => $brand_image,
                'brand_sort' => $brand_sort,
                'brand_cate_id' => ',' . $brand_cate_id . ',',
                'brand_status' => $brand_status,
                'brand_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_brand')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $cate = Db::table('up_cate')->where('cate_status', 1)->field('cate_id, cate_name')->select();
        return $this->fetch('', ['cate' => $cate]);
    }
    
    /**
     * 编辑品牌
     * @return mixed|\think\response\Json
     */
    public function brandEdit(){
        $brand_id = input('brand_id');
        if(Request::instance()->isPost()){
            $brand_name = trim(input('brand_name'));
            $brand_image = input('brand_image');
            $brand_sort = input('brand_sort');
            $brand_cate_id = input('brand_cate_id');
            $brand_status = input('brand_status');
    
            $update = [
                'brand_id' => $brand_id,
                'brand_name' => $brand_name,
                'brand_image' => $brand_image,
                'brand_sort' => $brand_sort,
                'brand_cate_id' => ',' . $brand_cate_id . ',',
                'brand_status' => $brand_status,
                'brand_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_brand')->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $brand = Db::table('up_brand')->where('brand_id', $brand_id)
            ->field('brand_name, brand_image, brand_sort, brand_cate_id, brand_status')
            ->find();
        $brand['brand_cate_id'] = trim($brand['brand_cate_id'], ',');
        $cate = Db::table('up_cate')->where('cate_status', 1)->field('cate_id, cate_name')->select();
        $this->assign([
            'brand' => $brand,
            'cate' => $cate,
            'brand_id' => $brand_id
        ]);
        return $this->fetch();
    }
    
    /**
     * 下架
     * @return \think\response\Json
     */
    public function brandDown(){
        $brand_id = input('id');
        $res = Db::table('up_brand')->where('brand_id', $brand_id)->setField('brand_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 上架
     * @return \think\response\Json
     */
    public function brandUp(){
        $brand_id = input('id');
        $res = Db::table('up_brand')->where('brand_id', $brand_id)->setField('brand_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 删除
     * @return \think\response\Json
     */
    public function brandDel(){
        $brand_id = input('id');
        $res = Db::table('up_brand')->where('brand_id', $brand_id)->setField('brand_status', 3);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 品牌排序
     * @return \think\response\Json
     */
    public function updateSort(){
        $brand_id = input('id');
        $field = input('field');
        $value = input('value');
        $res = Db::table('up_brand')->where('brand_id', $brand_id)->setField($field, $value);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 型号列表
     * @return mixed|\think\response\Json
     */
    public function modelList(){
        if(request()->isAjax ()){
            $models_name = trim(input('models_name'));
            $models_status = input('models_status');
            $models_cate_id = input('models_cate_id');
        
            $where['models_status'] = ['between', [1, 2]];
            $where['models_brand_id'] = session('brand_id');
            if(isset($models_name) && $models_name != ''){
                $where['models_name'] = ['like', '%' . $models_name . '%'];
            }
            if(isset($models_status) && $models_status != ''){
                $where['models_status'] = $models_status;
            }
            if(isset($models_cate_id) && $models_cate_id != ''){
                $where['models_cate_id'] = $models_cate_id;
            }
        
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('models')
                ->join('up_cate', 'cate_id = models_cate_id', 'LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('models')
                ->join('up_cate', 'cate_id = models_cate_id', 'LEFT')
                ->field('models_id, models_name, models_image, models_max_recycle_price, models_color, 
                models_sort, models_status, cate_name')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('models_sort desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $brand_id = input('brand_id');
        if(isset($brand_id) && $brand_id != ''){
            session('brand_id', $brand_id);
        }
        $cate = Db::table('up_cate')->where('cate_status', 1)->field('cate_id, cate_name')->select();
        $this->assign('cate', $cate);
        return $this->fetch();
    }
    
    /**
     * 添加型号
     * @return mixed|\think\response\Json
     */
    public function modelAdd(){
        if(Request::instance()->isPost()){
//            $models_brand_id = input('models_brand_id');
            $models_cate_id = input('models_cate_id');
            $models_name = trim(input('models_name'));
            $models_all_name = trim(input('models_all_name'));
            $models_image = input('models_image');
            $models_prime_price = trim(input('models_prime_price'));
            $models_max_recycle_price = trim(input('models_max_recycle_price'));
            $models_color = input('models_color');
            $models_sort = input('models_sort');
            $models_status = input('models_status');
            
            $insert = [
                'models_brand_id' => session('brand_id'),
                'models_cate_id' => $models_cate_id,
                'models_name' => $models_name,
                'models_all_name' => $models_all_name,
                'models_image' => $models_image,
                'models_prime_price' => $models_prime_price,
                'models_max_recycle_price' => $models_max_recycle_price,
                'models_color' => $models_color,
                'models_sort' => $models_sort,
                'models_status' => $models_status,
                'models_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_models')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $cate = Db::table('up_cate')->where('cate_status', 1)->field('cate_id, cate_name')->select();
//        $brand = Db::table('up_brand')->where('brand_status', 1)->field('brand_id, brand_name')->select();
        $model = Db::table('up_model_name')->field('mn_id, mn_name')->select();
        $this->assign([
            'cate' => $cate,
            'model' => $model,
//            'brand' => $brand
        ]);
        return $this->fetch();
    }
    
    /**
     * 编辑型号
     * @return mixed|\think\response\Json
     */
    public function modelEdit(){
        $models_id = input('models_id');
        if(Request::instance()->isPost()){
            $models_cate_id = input('models_cate_id');
            $models_name = trim(input('models_name'));
            $models_all_name = trim(input('models_all_name'));
            $models_image = input('models_image');
            $models_prime_price = trim(input('models_prime_price'));
            $models_max_recycle_price = trim(input('models_max_recycle_price'));
            $models_color = input('models_color');
            $models_sort = input('models_sort');
            $models_status = input('models_status');
    
            $update = [
                'models_id' => $models_id,
                'models_cate_id' => $models_cate_id,
                'models_name' => $models_name,
                'models_all_name' => $models_all_name,
                'models_image' => $models_image,
                'models_prime_price' => $models_prime_price,
                'models_max_recycle_price' => $models_max_recycle_price,
                'models_color' => $models_color,
                'models_sort' => $models_sort,
                'models_status' => $models_status,
                'models_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_models')->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $models = Db::table('up_models')
            ->where('models_id', $models_id)
            ->field('models_cate_id, models_name, models_all_name, models_image, models_prime_price, 
            models_max_recycle_price, models_color, models_sort, models_status')
            ->find();
        $cate = Db::table('up_cate')->where('cate_status', 1)->field('cate_id, cate_name')->select();
        $model = Db::table('up_model_name')->field('mn_id, mn_name')->select();
        $this->assign([
            'models' => $models,
            'cate' => $cate,
            'models_id' => $models_id,
            'model' => $model
        ]);
        return $this->fetch();
    }
    
    /**
     * 下架型号
     * @return \think\response\Json
     */
    public function modelDown(){
        $models_id = input('id');
        $res = Db::table('up_models')->where('models_id', $models_id)->setField('models_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 上架型号
     * @return \think\response\Json
     */
    public function modelUp(){
        $models_id = input('id');
        $res = Db::table('up_models')->where('models_id', $models_id)->setField('models_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 删除型号
     * @return \think\response\Json
     */
    public function modelDel(){
        $models_id = input('id');
        $res = Db::table('up_models')->where('models_id', $models_id)->setField('models_status', 3);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 型号排序
     * @return \think\response\Json
     */
    public function updateModelsSort(){
        $models_id = input('id');
        $field = input('field');
        $value = input('value');
        $res = Db::table('up_models')->where('models_id', $models_id)->setField($field, $value);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
}